package com.example.blog.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.UUID;
@Slf4j
public class SecurityUtils {

    public static String encrypt(String password) throws UnsupportedEncodingException {
        String salt = UUID.randomUUID().toString().replace("-","");
        String secretString = DigestUtils.md5DigestAsHex((salt+password).getBytes("UTF-8"));
        return salt+secretString;
    }

    public static Boolean verify(String inputPassword,String sqlPassword) {
        String salt = sqlPassword.substring(0,32);
        String secretString = null;
        try {
            secretString = DigestUtils.md5DigestAsHex((salt+inputPassword).getBytes("UTF-8"));
            String sqlPassword2 = salt+secretString;
            return sqlPassword.equals(sqlPassword2);
        } catch (UnsupportedEncodingException e) {
            log.info("字符串转换格式错误");
            return false;
        }
    }
}
